<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        /* 模态框背景 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }

        /* 模态框内容 */
        .modal-content {
            background-color: #f1f1f1;
            width: 300px;
            padding: 20px;
            border-radius: 5px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        /* 进度条容器 */
        .progress-container {
            width: 100%;
            height: 30px;
            background-color: #f1f1f1;
            margin-top: 10px;
            border: 1px solid #ccc;
            /* 添加外框 */
        }

        /* 进度条本身 */
        .progress-bar {
            width: 0;
            height: 100%;
            background-color: #4caf50;
        }

        /* 显示进度数字 */
        .progress-text {
            text-align: center;
            margin-top: 5px;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            收货验证
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 发货单号" id="consignment" value=""
                            onclick="getIdSelector();" >
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">送检位置</div>
                    <select id="uda1">
                    </select>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描托码" id="trayInfo" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码</div>
                    <input type="text" class="aui-input" id="tm">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan "></i>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <input type="text" class="aui-input" id="mrlCode" disabled="true">
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width:60%">总箱数</div>
                    <input type="text" class="aui-input" id="allBoxQty" disabled="true">
                </div>
                <div class="aui-list-item-inner" style="margin-left:-5%">
                    <div class="aui-list-item-label" style="width:100%">已验证箱数</div>
                    <input type="text" class="aui-input" id="okBoxQty" disabled="true">
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="lotCode" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <input type="text" class="aui-input" id="qty" disabled="true">
                </div>
            </li> -->
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li> -->
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div> -->

        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:25% ;margin-left:10%;" id="submitButton" onclick="createUqcmChkBillNew()">创建送检单
        </div>
        <div class="aui-btn aui-btn-info" style="width:25%" id="checkno" onclick="oneKeyConfirm()">一键确认</div>

        <!-- <div class="aui-btn aui-btn-info" style="margin-left:5%;width:25%" onclick="check()">校验</div> -->
        <div class="aui-btn aui-btn-info" style="width:25%" id="returnGoodsbtn" onclick="returnGoods()">来料拒收</div>
        <!-- <div class="aui-btn aui-btn-info" style="width:23%" onclick="toPrint()">补打</div> -->
        <!-- <div style="width:100%;margin-top:2%;"></div> -->
        <!-- <div class="aui-btn aui-btn-info" style="width:24%" id="createTraybtn" onclick="accessBackend()">创建托码</div>
        <div class="aui-btn aui-btn-info" style="width:24%" id="printTrayOldbtn" onclick="printTrayOld()">补打托码</div> -->
        <!-- <div class="aui-btn aui-btn-info" style="margin-left:5%;width:25%" onclick="toPage('3')">放行</div> -->
        <!-- <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;"> -->
        <div
            style="background-color:white; width: 100%;margin-top:2%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch;">

            <table class="table" style='width:1450px;' id="table1">
                <tbody style="display:block">
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center' style="width:40px;"></td>
                        <td algin='center' style="width:50px;">序号</td>
                        <td algin='center' style="width:133px;;">箱号</td>
                        <td algin='center' style="width:147px;">验证状态</td>
                        <td algin='center' style="width:70px;">检测单</td>
                        <td algin='center' style="width:80px;">验证结果</td>
                        <!-- <td algin='center' style="width:133px;">托码</td> -->
                        <td algin='center' style="width:150px;">物料号</td>
                        <td algin='center' style="width:150px;">规格</td>
                        <td algin='center' style="width:152px;">物料名称</td>
                        <td algin='center' style="width:102px;">批次号</td>
                        <td algin='center' style="width:80px;">数量</td>
                        <td algin='center' style="width:146px;">上次收货批次</td>
                        <td style="display:none;">验证状态编码</td>
                        <td style="display:none;">条码</td>
                        <td style="display:none;">id</td>
                    </tr>
                </tbody>
            </table>
            <table class="table" style='width:1450px;' id="table">
                <tbody id="tbodyHtml" style="display: block;height: 300px;overflow-y: scroll;">
                </tbody>
            </table>
        </div>
        <!-- 模态框 -->
        <div id="progressModal" class="modal">
            <div class="modal-content">
                <h2>正在加载...</h2>
                <div class="progress-container">
                    <div class="progress-bar" id="modalProgressBar"></div>
                </div>
                <div class="progress-text" id="progressText">0%</div>
            </div>
        </div>
    </div>
    <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    const showModalBtn = document.getElementById('showModalBtn');
    const progressModal = document.getElementById('progressModal');
    const modalProgressBar = document.getElementById('modalProgressBar');
    const progressText = document.getElementById('progressText');
    //光标所在id
    // var idSelector = "";
    var glType = "";
    var scanRule = ""; //条码解析规则
    var allConfirm = "";//一键确认标识
    var tmConfirm = ""; //扫箱确认标识

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }
    function updateModalProgressBar(progress) {
        modalProgressBar.style.width = progress + '%';
        progressText.textContent = progress + '%';
    }
    //初始化查询仓库下拉框
    apiready = function () {

        $("#consignment").focus();
        idSelector = "consignment";
        var query = api.pageParam.query;
        glType = query.glType

        console.log(glType)

        if (glType != "LJ") {
            //      $("#checkno").css('display', 'none');
            //  $("#checkno").attr('disabled', true);
            $("#returnGoodsbtn").css({ "width": "32%" });
            $("#createTraybtn").css({ "width": "32%" });
            $("#printTrayOldbtn").css({ "width": "32%" });
        }

        //查询检验库下拉框
        // doInit();

        //查询打印机下拉框
        // selectPrintName();

        //查询条码解析规则
        initScanRule();

        //获取蓝牙列表
        // getBluetoothCombox();

        //查询送检位置
        selectUda1();
    }

    //查询送检位置下拉框
    function selectUda1() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getDJworkCellList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "<option value=''>--请选择--</option>";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                }
                $api.html($api.byId("uda1"), html);
            }
        });

    }

    //查询检验库编码
    // function doInit() {
    //     var serverUrl = localStorage.getItem('serverUrl');
    // var url = serverUrl + "/app/getWorkCenterJYK";//后台方法暂无
    //     var data = {
    //         values: { ////以表单方式提交参数（JSON对象）
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function(ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var map = eval("(" + data + ")");
    //             var list = eval(map.list);
    //             var html = "<option value=''>--请选择--</option>";
    //             for (var i = 0; i < list.length; i++) {
    //                 html += "<option value='" + list[i].gid + "'>" + list[i].workcenterName + "</option>";
    //             }
    //             $api.html($api.byId("workCenterGid"), html);
    //         }
    //     });
    // }

    // //查询打印机下拉框
    // function selectPrintName() {
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/getServicePrinterList";//后台方法暂无
    //     var data = {
    //         values: { ////以表单方式提交参数（JSON对象）
    //             un: un
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function(ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var map = eval("(" + data + ")");
    //             var list = eval(map.list);
    //             var html = "<option value=''>--请选择--</option>";
    //             for (var i = 0; i < list.length; i++) {
    //                 html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
    //             }
    //             $api.html($api.byId("printName"), html);
    //         }
    //     });
    // }

    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.searchBluetooth({}, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        // console.log(address);
        // api.showProgress({
        //     title: '连接中...',
        // });
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.connectBluetooth({
            address: address,
        }, function (ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            //1：发货单号
            if (idSelector == 'consignment') {

                //发货单号
                $("#consignment").val(ret);

                //查询表格数据
                onChangeConsignment();

                //2:箱号
            } else if (idSelector == 'boxnum') {
                $("#boxnum").blur();

                //条码解析+箱号值改变
                tmSplit(ret);
            } else if (idSelector == 'trayInfo') {
                $("#trayInfo").blur();

                //托码值改变
                onChangeTrayInfo(ret);
            }
        });
    }

    //发货单监听回车事件
    $("#consignment").on('keypress', function (e) { //#keyword为input文本框
        console.log(111);
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') {
            if (consignment != null && consignment != "" && consignment != undefined) {
                console.log(222);
                //发货单
                $("#consignment").val(consignment);

                //箱号值改变
                onChangeConsignment();
            }


        }
    });

    //条码解析+箱号值改变
    function tmSplit(sm) {

        //条码
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        console.log(arr);
        if (arr.length < 10) {
            api.toast({
                msg: "条码规则不正确，请重新扫码！",
                duration: 3000
            });
            alert("1")
            return;
        }
        //物料号
        $("#mrlCode").val(arr[1]);
        //数量
        $("#qty").val(arr[2]);
        //批次号
        $("#lotCode").val(arr[3]);
        //箱号
        $("#boxnum").val(arr[9]);

        //箱号选中
        $("#boxnum").select();

        //箱号值改变
        onChangeBoxnum(sm);
    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    if ($("#boxnum").val() == "" || $("#consignment").val() == null) {
                        api.toast({
                            msg: "条码规则不正确，请重新扫码！",
                            duration: 3000
                        });
                        return;
                    }
                    //箱号选中
                    $("#boxnum").select();

                    //箱号值改变
                    onChangeBoxnum(sm);
                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#tm").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#boxnum").val("");
                $("#tm").val("");
                $("#boxnum").select();
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#boxnum").blur();
                console.log(tm)
                //条码解析+箱号值改变
                tmSplit(tm);
            }

        }
    });
    //托码监听回车事件
    $("#trayInfo").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#trayInfo").blur();
                console.log(tm);

                //托码值改变
                onChangeTrayInfo(tm);
            }



        }
    });
    //发货单号值改变
    function onChangeConsignment() {
        console.log(333);
        //取发货单号
        var consignment = $("#consignment").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {

            $api.html($api.byId("tbodyHtml"), "");

            //光标
            $("#consignment").focus();
            idSelector = "consignment";

            //不为空，
        } else {
            console.log(444);
            //查询有效期
            periodOfVaildity(consignment)
            
        }
    }
    //保质期、有效期查询
    function periodOfVaildity(consignment,sm) {

        const submitButton = document.getElementById('checkno');
        submitButton.style.pointerEvents = "none"; // 禁用一键确认按钮

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/periodOfVaildity";
        var data = {
            values: {
                consignment: consignment,
                un:un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {

                    submitButton.style.pointerEvents = "auto"; // 启用一键确认按钮

                    if (myobj.flag != 0) { //过期标识 0-未过期
                        api.toast({
                            msg: myobj.msg,
                            duration: 3000
                        });
                        return
                    }
                    if (myobj.type != 0) { //预警标识 0-无需预警
                        var dialogBox = api.require('dialogBox');
                        dialogBox.alert({
                            texts: {
                                title: '',
                                content: myobj.msg + ',是否继续发货？',
                                leftBtnTitle: '否',
                                rightBtnTitle: '是'
                            },
                            styles: {
                                bg: '#fff',
                                w: 300,
                                corner: 2,
                                title: {
                                    marginT: 20,
                                    // icon: 'widget://res/gou.png',
                                    iconSize: 40,
                                    titleSize: 14,
                                    titleColor: '#000'
                                },
                                content: {
                                    color: '#000',
                                    size: 14
                                },
                                left: {
                                    marginB: 7,
                                    marginL: 20,
                                    w: 130,
                                    h: 35,
                                    corner: 2,
                                    bg: 'rgba(0,0,0,0)',
                                    size: 12
                                },
                                right: {
                                    marginB: 7,
                                    marginL: 10,
                                    w: 130,
                                    h: 35,
                                    corner: 2,
                                    bg: 'rgba(0,0,0,0)',
                                    size: 12
                                }
                            }
                        }, function (ret) {
                            //否
                            if (ret.eventType == 'left') {
                                var dialogBox = api.require('dialogBox');
                                dialogBox.close({
                                    dialogName: 'alert'
                                });
                                $("#allBoxQty").val("");
                                $("#okBoxQty").val("");
                                $("#consignment").val("");
                                $("#consignment").focus();
                                $("#boxnum").val("");
                                idSelector = "consignment";
                                //清空表格数据
                                $api.html($api.byId("tbodyHtml"), "");
                            //是
                            } else {
                                var dialogBox = api.require('dialogBox');
                                dialogBox.close({
                                    dialogName: 'alert'
                                });
                                searchDetailsBeforCheck(consignment)
                                if(tmConfirm == "1"){
                                    onChangeBoxnum2(sm)
                                }
                                if(allConfirm == "1"){
                                    oneKeyConfirm2(consignment)
                                }
                               
                            }
                        });
                    }else{
                        searchDetailsBeforCheck(consignment)
                        if(tmConfirm == "1"){
                            onChangeBoxnum2(sm)
                        }
                        if(allConfirm == "1"){
                            oneKeyConfirm2(consignment)
                        }
                    }

                    //查询表格数据
                    //searchDetailsBeforCheck(consignment);
                    //失败
                } else if (myobj.code == "1") {

                    submitButton.style.pointerEvents = "auto"; // 启用一键确认按钮

                    $("#consignment").val("");
                    $("#consignment").focus();
                    idSelector = "consignment";

                    console.log('失败信息')

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });
    }
    //根据发货单查询表格数据
    function searchDetailsBeforCheck(consignment) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getConsignmentCheck";
        var data = {
            values: {
                consignment: consignment,
                un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    if (myobj.codeQ == "1") {
                        api.toast({
                            msg: myobj.message,
                            duration: 10000
                        });
                    }

                    searchDetails(consignment);

                    //失败
                } else if (myobj.code == "1") {

                    $("#consignment").select();
                    idSelector = "consignment";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //根据发货单查询表格数据
    function searchDetails(consignment, inputType) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getConsignment";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                consignment: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);

                    var html = "";

                    var num = 0;

                    //发货单明细数据存在
                    if (list.length > 0) {

                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                            html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                            html += "<td name='seqNo' style='width:50px;'>" + (i + 1) + "</td>";
                            html += "<td name='boxnum' style='width:133px;'>" + list[i].boxnum + "</td>";
                            //验证状态 0待检/1OK/2NG/3放行/4退货/5免检
                            if (list[i].gyStatus == '0') {
                                //0待检
                                html += "<td name='gyStatusName' style='background-color:red;width:147px;'>" + list[i].gyStatusName + "</td>";
                            } else if (list[i].gyStatus == '2' || list[i].gyStatus == '3' || list[i].gyStatus == '4') {
                                //2NG/3放行/4退货
                                html += "<td name='gyStatusName' style='background-color:yellow;width:147px;'>" + list[i].gyStatusName + "</td>";
                            } else {
                                html += "<td name='gyStatusName' style='width:147px;'>" + list[i].gyStatusName + "</td>";
                            }
                            if (list[i].checkItem == "已创建") {
                                html += "<td name='checkItem' style='width:70px;'>" + list[i].checkItem + "</td>";
                            } else {
                                html += "<td name='checkItem' style='background-color:red;width:70px;'>" + list[i].checkItem + "</td>";
                            }
                            if (list[i].gyCheck == '0') {
                                html += "<td name='gyCheckName'  style='background-color:green;width:80px;'>" + (list[i].gyCheckName == null ? "" : list[i].gyCheckName) + "</td>";
                                num++;
                            } else {
                                html += "<td name='gyCheckName'  style='background-color:red;width:80px;'>待验证</td>";
                            }
                            //console.log(list[i].trayId)
                            // html += "<td name='trayId' style='width:133px;'>" + (list[i].trayId == null ? "" : list[i].trayId) + "</td>";

                            html += "<td name='mrlCode' style='width:150px;'>" + list[i].mrlCode + "</td>";
                            html += "<td name='mrlCode' style='width:150px;'>" + list[i].drawing + "</td>";
                            html += "<td name='mrlCode' style='width:152px;'>" + list[i].mrlName + "</td>";
                            html += "<td name='lotCode' style='width:102px;'>" + list[i].lotCode + "</td>";
                            html += "<td name='qty' style='width:80px;'>" + list[i].qty + "</td>";
                            html += "<td name='lastLotCode' style='width:146px;'>" + (list[i].lastLotCode == null ? "" : list[i].lastLotCode) + "</td>";




                            html += "<td name='gyStatus' style='display:none;'>" + list[i].gyStatus + "</td>";
                            html += "<td name='tm' style='display:none;'>" + list[i].tm + "</td>";
                            html += "<td name='id' style='display:none;'>" + list[i].id + "</td>";
                            // html += "<td name='checkItem'>" + list[i].checkItem + "</td>";
                            html += "</tr>";
                        }
                        $("#allBoxQty").val(list.length);
                        $("#okBoxQty").val(num);
                        console.log("光标箱码")
                        if (inputType == "trayInfo") {
                            $("#trayInfo").focus();
                            $("#trayInfo").select();
                            idSelector = "trayInfo";
                        } else {
                            $("#boxnum").focus();
                            $("#boxnum").select();
                            idSelector = "boxnum";
                        }

                    } else {

                        $("#consignment").focus();
                        $("#consignment").select();
                        idSelector = "consignment";
                    }

                    $api.html($api.byId("tbodyHtml"), html);

                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    $("#consignment").select();
                    idSelector = "consignment";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫码条码箱号校验
    function onChangeBoxnum(sm) {
        var consignment = $("#consignment").val();
        if (consignment != "" && consignment != null && consignment != undefined) {
            tmConfirm = "1";
            periodOfVaildity(consignment,sm)
     
        }
    }
    function onChangeBoxnum2(sm) {
        
        var consignment = $("#consignment").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/checkTM";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                consignment: consignment,
                tm: sm,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                tmConfirm = "0"
                //成功
                if (myobj.code == "0") {

                    $("#tm").val("");
                    $("#boxnum").val("");
                    $("#mrlCode").val("");
                    $("#lotCode").val("");
                    $("#qty").val("");
                    //刷新表格数据
                    searchDetails(consignment);
                    api.toast({
                        msg: "校验成功",
                        duration: 3000
                    });

                    //失败
                } else if (myobj.code == "1") {

                    searchDetails(consignment);
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    //看板明细存在无采购订单数据
                } else if (myobj.code == "2") {
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector = "boxnum";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#boxnum").val("");
                $("#boxnum").select();
                idSelector = "boxnum";
                api.hideProgress();
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //校验
    // function check() {
    //
    //     //条码
    //     var tm = $("#tm").val();
    //     //箱号
    //     var boxnum = $("#boxnum").val();
    //     //物料号
    //     var mrlCode = $("#mrlCode").val();
    //     //发货单号
    //     var consignment = $("#consignment").val();
    //
    //     //发货单号为空校验
    //     if (consignment == null || consignment == undefined || consignment == "") {
    //         api.toast({
    //             msg: "发货单号不能为空",
    //             duration: 3000
    //         });
    //         return;
    //     }
    //
    //     //条码非空校验
    //     if (boxnum == null || boxnum == undefined || boxnum == "") {
    //         api.toast({
    //             msg: "箱号不能为空",
    //             duration: 3000
    //         });
    //         return;
    //     }
    //
    //     //条码非空校验
    //     if (mrlCode == null || mrlCode == undefined || mrlCode == "") {
    //         api.toast({
    //             msg: "物料号不能为空",
    //             duration: 3000
    //         });
    //         return;
    //     }
    //     onChangeBoxnum(tm);
    // }

    //让步放行，退回按钮事件
    function toPage(flag) {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });

        //选中行不为1
        if (count != 1) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }

        var id = "";
        var gyStatus = "";
        // var purchaseOrder = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            id = row.find("[name='id']").html(); //注意html()和val()
            gyStatus = row.find("[name='gyStatus']").html();
        });

        //1:放行:只能选中0待检和和2NG和4退或的
        if (flag == '3') {

            //判断状态
            if (gyStatus == "1") {
                api.toast({
                    msg: '状态必须是待检/NG/放行/退货!',
                    duration: 3000
                });
                return;
            }

            //2:退回:待检和NG
        } else if (flag == '4') {
            //判断状态
            if (gyStatus == "1" || gyStatus == '3' || gyStatus == '4') {
                api.toast({
                    msg: '状态必须是待检/NG!',
                    duration: 3000
                });
                return;
            }
        }

        //开发验证详细页面
        api.openWin({
            name: 'uwmSHVerification_input_djg',
            url: 'uwmSHVerification_input_djg.html',
            pageParam: {
                id: id,
                gyStatus: flag,
                name: api.winName,
            }
        });
    }

    //页面刷新
    function refresh(consignment) {
        searchDetails(consignment);
    }

    //创建送检单、检测单
    function createUqcmChkBillNew() {
         if (!clickThrottle()) {
            return;
        }


        //发货单号
        var consignment = $("#consignment").val();
        //送检位置
        var uda1 = $("#uda1").val();
        console.log(uda1)

        //发货单号非空校验
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            return;
        }
        //送检位置非空校验
        if (uda1 == "" || uda1 == null || uda1 == undefined) {
            api.toast({
                msg: "送检位置不能为空",
                duration: 1000
            });
            return;
        }

        const submitButton = document.getElementById('submitButton');
        submitButton.style.pointerEvents = "none"; // 禁用提交按钮

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/createUqcmChkBillJT";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                consignment: consignment, //发货单号
                uda1: uda1, //送检位置
                un: un
            }
        };
        api.showProgress({
            title: '创建中...',
        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
           
            if (ret) {
                var myobj = ret;
                //成功
                if (myobj.code == "0") {
                    submitButton.style.pointerEvents = "auto"; // 启用提交按钮
                    api.hideProgress();
                    api.toast({
                        msg: "送检单创建成功！",
                        duration: 3000
                    });
                    //刷新表格
                    searchDetails(consignment);
                    //失败,模板存在性校验
                } else if (myobj.code == "1") {
                    submitButton.style.pointerEvents = "auto"; // 启用提交按钮
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            }else{
                submitButton.style.pointerEvents = "auto"; // 启用提交按钮
            }
        });
    }

    //一键确认 把发货单下所有箱的 收货验证状态=1-OK  收货验证结果=0-OK
    function oneKeyConfirm() {
        if (!clickThrottle()) {
            return;
        }

        const submitButton = document.getElementById('checkno');
        submitButton.style.pointerEvents = "none"; // 禁用一键确认按钮

        //发货单号
        var consignment = $("#consignment").val();

        //发货单号非空校验
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"; // 启用一键确认按钮
            return;
        }else{
       
            allConfirm = "1"
            periodOfVaildity(consignment)
        }
    }  
    function oneKeyConfirm2(consignment) {

        api.showProgress({
            title: '确认中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/oneKeyConfirm";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                consignment: consignment, //发货单号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                allConfirm = "0"
                var myobj = ret;
                //成功
                if (myobj.code == "0") {

                    api.hideProgress();
                    api.toast({
                        msg: "一键确认成功！",
                        duration: 700
                    });
                    //刷新表格
                    searchDetails(consignment);

                    //失败,模板存在性校验
                } else if (myobj.code == "1") {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }

    // //退货
    // function returnGoods() {
    //     console.log(11111);
    //     //发货单号
    //     var consignment = $("#consignment").val();

    //     //选中数据
    //     var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
    //     var count = 0;
    //     var id = "";//发货单条码内容表id
    //     var gyStatus = "";//收货验证状态
    //     check.each(function () { //遍历
    //         var row = $(this).parent("td").parent("tr");
    //         id = row.find("[name='id']").html(); //注意html()和val()
    //         gyStatus = row.find("[name='gyStatus']").html();
    //         count++;
    //     });
    //     if (count != 1) {
    //         api.toast({
    //             msg: '请选择一条表格数据！',
    //             duration: 3000
    //         });
    //         return;
    //     }

    //     if (gyStatus == '4') {
    //         api.toast({
    //             msg: '请箱已经退货！',
    //             duration: 3000
    //         });
    //         return;
    //     }

    //     if (gyStatus == '1') {
    //         api.toast({
    //             msg: '请箱已经校验通过，不能退货！',
    //             duration: 3000
    //         });
    //         return;
    //     }

    //     /*    if (gyStatus == '5') {
    //            api.toast({
    //                msg: '请箱已免检，不能退货！',
    //                duration: 3000
    //            });
    //            return;
    //        } */
    //        api.confirm({
    // 			title: '提示',
    // 			msg: "是否进行退货，退货后选中的箱信息将在发货单中删除!",
    // 			buttons: ['确定', '取消']
    // 	}, function (ret, err) {
    // 			var index = ret.buttonIndex;
    // 			if (index == 1) {
    // 				api.showProgress({
    // 						title: '保存中...',
    // 				});
    //     console.log(11112);
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/returnGoods";
    //     var params = new Object();
    //     params.url = url;
    //     params.dataType = 'json';
    //     params.method = 'post';
    //     var data = {
    //         values: {
    //             id: id, //条码内容id
    //             un: un
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function (ret, err) {
    //         if (ret) {
    //             var myobj = ret;

    //             //成功
    //             if (myobj.code == "0") {

    //                 api.hideProgress();
    //                 api.toast({
    //                     msg: "退货成功！",
    //                     duration: 3000
    //                 });
    //                 //刷新表格
    //                 searchDetails(consignment);

    //                 //失败
    //             } else if (myobj.code == "1") {
    //                 api.hideProgress();
    //                 api.toast({
    //                     msg: myobj.message,
    //                     duration: 5000
    //                 });
    //             }
    //         } else {
    //             api.hideProgress();
    //         }
    //              });
    //           }
    // 		}
    // 	)
    // }

    //退货
    function returnGoods() {
        //发货单号
        var consignment = $("#consignment").val();
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        var flag = "";
        check.each(function () { //遍历

            var row = $(this).parent("td").parent("tr");
            var gyStatus = row.find("[name='gyStatus']").html();
            var boxnum = row.find("[name='boxnum']").html();
            count++;
            if (gyStatus == '1') {
                alert('箱【' + boxnum + '】已经校验通过，不能退货！');
                flag = "E"
                return;
            }
            if (gyStatus == '4') {
                alert('箱【' + boxnum + '】已经退货！');
                flag = "E"
                return;
            }

        });
        if (flag == "E") {
            return;
        }
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }


        var list = [];

        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            var boxnum = row.find("[name='boxnum']").html();
            var mrlCode = row.find("[name='mrlCode']").html();
            var lotCode = row.find("[name='lotCode']").html();
            var qty = row.find("[name='qty']").html();

            list.push({
                "boxnum": boxnum,
                "mrlCode": mrlCode,
                "lotCode": lotCode,
                "qty": qty,
            });
        });
        //打开页面
        api.openWin({
            name: 'deliveryRefused',
            url: 'deliveryRefused' + '.html',
            pageParam: {
                consignment: consignment,
                list: list,
            },
        });

    }

    //补打
    function toPrint() {
        var address = $("#bluetooth").val();

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        var boxnum = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            boxnum = row.find("[name='boxnum']").html();
            count++;
        });
        if (count != 1) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }
        localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/tmBoxnumPlatform";
        var data = {
            values: {
                boxnum: boxnum,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    //返回json数据
                    var json = myobj.json;

                    //调用PDA打印
                    printPDA(json, address);

                } else {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                $("#boxnum").val("");
                $("#boxnum").select();
                idSelector = "boxnum";
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }
    //打印
    function printPDA(json, address) {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        modulePrinterCpclDJG.printerPlatform({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });



                } else {


                    api.toast({
                        msg: ret.message
                    });
                }
            } else {

                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }
    // //补打条码
    // function toPrint() {
    //     var printName = $("#printName").val();
    //     if (printName == null || printName == "" || printName == undefined) {
    //         api.toast({
    //             msg: '请选择打印机！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //
    //     //选中数据
    //     var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
    //     var count = 0;
    //     check.each(function() {
    //         count++;
    //     });
    //
    //     //选中行不为1
    //     if (count != 1) {
    //         api.toast({
    //             msg: '请选择一条表格数据！',
    //             duration: 3000
    //         });
    //         return;
    //     }
    //     //条码
    //     var tm = "";
    //     check.each(function() { //遍历
    //         var row = $(this).parent("td").parent("tr");
    //         tm = row.find("[name='tm']").html();
    //     });
    //
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/print";
    //     var data = {
    //         values: {
    //             tm: tm,
    //             printName: printName,
    //             un: un
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function(ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var myobj = eval("(" + data + ")");
    //             //成功
    //             if (myobj.code == "0") {
    //
    //                 //成功
    //                 api.toast({
    //                     msg: "打印成功！",
    //                     duration: 3000
    //                 });
    //
    //                 //失败,模板存在性校验
    //             } else if (myobj.code == "1") {
    //                 api.toast({
    //                     msg: myobj.message,
    //                     duration: 5000
    //                 });
    //             }
    //         }
    //     });
    // }

    // //检验库仓库下拉框值改变
    // function onChangeWorkCenter() {
    //     var workCenterGid = $("#workCenterGid").val();
    //     //源仓库不为空，查询库位下拉框
    //     if (workCenterGid != "" && workCenterGid != null && workCenterGid != undefined) {
    //         var un = localStorage.getItem('un');
    //         var serverUrl = localStorage.getItem('serverUrl');
    //         var url = serverUrl + "appUwmController!getWorkCell.m";
    //         var params = new Object();
    //         params.url = url;
    //         params.dataType = 'json';
    //         params.method = 'post';
    //         var data = {
    //             values: { ////以表单方式提交参数（JSON对象）
    //                 flag: "jyk",
    //                 workCenterGid: workCenterGid,
    //                 un: un
    //             }
    //         };
    //         ajaxForData(params, data, false, function(ret, err) {
    //             if (ret) {
    //                 var myobj = eval("(" + ret.data + ")");
    //                 if (myobj.code == "0") {
    //                     var list = myobj.list;
    //                     var html = "<option value=''>--请选择--</option>";
    //                     for (var i = 0; i < list.length; i++) {
    //                         html += "<option value='" + list[i].workCellGid + "'>" + list[i].workCellName + "</option>";
    //                     }
    //                     $api.html($api.byId("workCellGid"), html);
    //                 }
    //             }
    //         });
    //     } else {
    //         $api.html($api.byId("workCellGid"), "");
    //     }
    // }

    //打印
    function createTray() {

        var address = $("#bluetooth").val();
        //发货单号
        var consignment = $("#consignment").val();

        //发货单号非空校验
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "请先扫描发货单！",
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var tb = document.getElementById("table");
        var rows = tb.rows;
        var num = 0;

        for (var i = 0; i < rows.length; i++) {    //--循环所有的行
            var cells = rows[i].cells;　　// cells 集合返回表格中所有 <td> 或 <th> 元素。
            var value = cells[2].innerHTML;
            console.log(value);
            var value2 = "";
            value2 = cells[3].innerHTML;
            console.log(value2);
            if (value == "OK" && (value2 == "" || value2 == null)) {
                num++;
            }
        }

        if (num == 0) {
            api.toast({
                msg: "该发货单下未查询到已验证并且未绑定的箱码！",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmTrayinfoCreateNew";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    //返回托码数据
                    var trayId = myobj.boxnum;

                    // 调用PDA打印
                    // bindTray(trayId, consignment);

                } else {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    // async function accessBackend() {
    //     progressModal.style.display = 'block'; // 显示模态框

    //     // 开始访问后台
    //     let progress = 0;
    //     const interval = setInterval(async function () {
    //         if (progress < 50) {
    //             progress += 1;
    //             updateModalProgressBar(progress);
    //         } else {
    //             clearInterval(interval);

    //             await createAndBindTray();
    //             progress = 100;
    //             updateModalProgressBar(progress);
    //             await waitForHalfSecond();
    //             progressModal.style.display = 'none';

    //         }
    //     }, 50);

    // }
    // function waitForHalfSecond() {
    //     return new Promise(resolve => {
    //         setTimeout(() => {
    //             resolve();
    //         }, 500);
    //     });
    // }
    //绑定箱码
    function createAndBindTray(consignment) {

        var address = $("#bluetooth").val();
        //发货单号
        var consignment = $("#consignment").val();

        //发货单号非空校验
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "请先扫描发货单！",
                duration: 3000
            });
            return;
        }

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }

        localStorage.setItem('bluetooth', address);

        var tb = document.getElementById("table");
        var rows = tb.rows;
        var num = 0;

        for (var i = 0; i < rows.length; i++) {    //--循环所有的行
            var cells = rows[i].cells;　　// cells 集合返回表格中所有 <td> 或 <th> 元素。
            var value = cells[2].innerHTML;
            console.log(value);
            var value2 = "";
            value2 = cells[3].innerHTML;
            console.log(value2);
            if (value == "OK" && (value2 == "" || value2 == null)) {
                num++;
            }
        }

        if (num == 0) {
            api.toast({
                msg: "该发货单下未查询到已验证并且未绑定的箱码！",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmTrayinfoBindCon";
        var data = {
            values: {
                consignment: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var trayId = myobj.trayCode;
                    // 调用PDA打印
                    printTrayNew(trayId, address);
                    searchDetails(consignment);

                } else {

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //打印
    function printTrayNew(boxnum, address) {
        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        //打印托码
        modulePrinterCpclDJG.printerTrayinfo({
            boxnum: boxnum, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });

                } else {

                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                //alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //打印
    function printTrayOld() {

        var address = $("#bluetooth").val();

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙！"
            });
            return;
        }

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        var trayId = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            trayId = row.find("[name='trayId']").html();
            count++;
        });
        if (count > 1) {
            api.toast({
                msg: '不能多选，请只选择一条表格数据！',
                duration: 3000
            });
            return;
        } if (count == 0) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }

        if (trayId == "") {
            api.toast({
                msg: '该箱码未绑定托码，无法进行托码打印，请先绑定托码后再进行打印！！',
                duration: 3000
            });
            return;
        }
        localStorage.setItem('bluetooth', address);

        var modulePrinterCpclDJG = api.require('modulePrinterCpclDJG');
        //打印托码
        modulePrinterCpclDJG.printerTrayinfo({
            boxnum: trayId, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {

                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });

                } else {

                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //扫码托码校验
    function onChangeTrayInfo(sm) {
        var consignment = $("#consignment").val();
        if (consignment != "" && consignment != null && consignment != undefined) {
            var un = localStorage.getItem('un');
            var serverUrl = localStorage.getItem('serverUrl');
            var url = serverUrl + "/app/checkTrayInfo";
            var data = {
                values: { ////以表单方式提交参数（JSON对象）
                    consignment: consignment,
                    trayInfo: sm,
                    un: un
                }
            };
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 300,
                returnAll: false,
                data: data
            }, function (ret, err) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");

                    //成功
                    if (myobj.code == "0") {

                        $("#tm").val("");
                        $("#boxnum").val("");
                        $("#mrlCode").val("");
                        $("#lotCode").val("");
                        $("#qty").val("");
                        $("#trayInfo").val("");
                        //刷新表格数据
                        searchDetails(consignment, "trayInfo");

                        api.toast({
                            msg: "校验成功",
                            duration: 3000
                        });
                        $("#trayInfo").val("");
                        $("#trayInfo").focus();
                        idSelector = "trayInfo";
                        //失败
                    } else if (myobj.code == "1") {

                        searchDetails(consignment, "trayInfo");

                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                        $("#trayInfo").val("");
                        $("#trayInfo").focus();
                        idSelector = "trayInfo";
                        //看板明细存在无采购订单数据
                    } else if (myobj.code == "2") {
                        $("#trayInfo").val("");
                        $("#trayInfo").select();
                        idSelector = "trayInfo";
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }
                } else {
                    $("#trayInfo").val("");
                    $("#trayInfo").select();
                    idSelector = "trayInfo";
                    api.hideProgress();
                    //alert(JSON.stringify(err));
                    alert("连接超时，请耐心等待");
                }
            });
        }
    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>